package com.pda.xinma.ui.inventory;

import androidx.recyclerview.widget.RecyclerView;

import com.pda.xinma.R;
import com.pda.xinma.adapter.ExceptionalAdapter;
import com.pda.xinma.bean.ExceptionsListBean;
import com.pda.xinma.contract.ExceptionalContract;
import com.pda.xinma.presenter.ExceptionalPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ExceptionalFragment extends BaseMvpFragment<ExceptionalPresenter> implements ExceptionalContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    private String taskNo;
    ExceptionalAdapter exceptionalAdapter;
    private int pager=1;
    public ExceptionalFragment(String taskNo) {
        this.taskNo = taskNo;
    }

    public ExceptionalFragment() {
    }

    @Override
    public ExceptionalPresenter getPresenter() {
        return new ExceptionalPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list_scan;
    }

    @Override
    public void initView() {
        exceptionalAdapter = new ExceptionalAdapter(R.layout.item_inverntory_one, new ArrayList<>());
        rvContent.setAdapter(exceptionalAdapter);
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);
        srlLayout.autoRefresh();
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {

    }
    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }
    @Override
    public void setList(List<ExceptionsListBean> list) {
        if (pager == 1){
            exceptionalAdapter.setNewInstance(list);
        }else {
            exceptionalAdapter.addData(list);
        }
        srlLayout.finishLoadMore();
        srlLayout.finishRefresh();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager=1;
        presenter.exceptionsList(taskNo,pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.exceptionsList(taskNo,pager);
    }
}
